/******************************************************************
 * Copyright (c) 2007 - 2007, Simon Guerard, All rights reserved.
 * 
 * Projet de jeu de course au large.
 * 
 * This File is part of Sail-Race
 * 
 * Sail-Race is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 * 
 * Sail-Race is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Sail-Race; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

package net.simcafamily.sailrace.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import net.simcafamily.sailrace.bo.User;
import net.simcafamily.sailrace.dao.IUserDao;

import org.hsqldb.types.Types;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreatorFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.stereotype.Repository;

/**
 * @author Simon Guerard
 * 
 */
@Repository("userDao")
// @Transactional
public class UserDaoImpl implements IUserDao {

    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    /**
     * @see net.simcafamily.sailrace.dao.IUserDao#getUser(int)
     */
    @Override
    public User getUser(Integer id) {
	PreparedStatementCreatorFactory factory = new PreparedStatementCreatorFactory(
		"SELECT name FROM users WHERE id=?");
	factory.addParameter(new SqlParameter("id", Types.INTEGER));

	List<User> list = jdbcTemplate.query(
		factory.newPreparedStatementCreator(new Object[] { id }),
		new RowMapper<User>() {

		    @Override
		    public User mapRow(ResultSet rs, int rowNum)
			    throws SQLException {
			User user = new User();
			user.setNom(rs.getString("name"));
			return user;
		    }
		});
	if (list.size() > 0) {
	    return list.get(0);
	}
	return null;
    }

    /**
     * @see net.simcafamily.sailrace.dao.IUserDao#getUser(int)
     */
    @Override
    public User getUser(String name) {
	PreparedStatementCreatorFactory factory = new PreparedStatementCreatorFactory(
		"SELECT name FROM users WHERE name=?");
	factory.addParameter(new SqlParameter("name", Types.VARCHAR));

	List<User> list = jdbcTemplate.query(
		factory.newPreparedStatementCreator(new String[] { name }),
		new RowMapper<User>() {

		    @Override
		    public User mapRow(ResultSet rs, int rowNum)
			    throws SQLException {
			User user = new User();
			user.setNom(rs.getString("name"));
			return user;
		    }
		});
	if (list.size() > 0) {
	    return list.get(0);
	}
	return null;
    }

}
